package com.donger.web.admin.controller.upms;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.donger.business.modules.upms.entity.SysConfig;
import com.donger.business.modules.upms.service.ISysConfigService;
import com.donger.common.core.utils.Res;
import com.donger.common.core.utils.Result;
import com.donger.common.web.baseweb.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;


/**
 * <p>
 * 系统配置信息表 前端控制器
 * </p>
 *
 * @author xyx
 * @since 2019-01-12
 */
@AllArgsConstructor
@RestController
@RequestMapping("/sys/config")
@Api(tags = {"配置config"})
public class SysConfigController extends BaseController {

    private static final String MODULE_NAME = "系统配置管理";

    private final ISysConfigService sysConfigService;

    /**
     * 所有配置列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "配置列表")
    public Result list(Page<SysConfig> page, SysConfig entity) {
        return Res.ok(sysConfigService.page(page, new LambdaQueryWrapper<SysConfig>()
                .like(StrUtil.isNotBlank(entity.getParamKey()), SysConfig::getParamKey, entity.getParamKey())
        ));
    }

    /**
     * 配置信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "配置详情")
    public Result<SysConfig> info(@PathVariable("id") Long id) {
        return Res.ok(sysConfigService.getById(id));
    }

    /**
     * 保存配置
     */
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('sys:config:save')")
    public Result<Boolean> save(@Valid @RequestBody SysConfig config) {
        return Res.ok(sysConfigService.save(config));
    }

    /**
     * 修改配置
     */
    @PostMapping("/update")
    @PreAuthorize("@pms.hasPermission('sys:config:update')")
    public Result<Boolean> update(@Valid @RequestBody SysConfig config) {
        return Res.ok(sysConfigService.updateById(config));
    }

    /**
     * 删除配置
     */
    @DeleteMapping("/del/{id}")
    @PreAuthorize("@pms.hasPermission('sys:config:del')")
    public Result<Boolean> delete(@PathVariable Serializable id) {
        return Res.ok(sysConfigService.removeById(id));
    }


}

